{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a7e1253f-5cdb-4553-8c69-e5105172b6da",
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'tensorflow'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[1], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtensorflow\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m keras\n\u001b[1;32m      3\u001b[0m base_model \u001b[38;5;241m=\u001b[39m keras\u001b[38;5;241m.\u001b[39mapplications\u001b[38;5;241m.\u001b[39mVGG16(\n\u001b[1;32m      4\u001b[0m     weights\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mimagenet\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m      5\u001b[0m     input_shape\u001b[38;5;241m=\u001b[39m(\u001b[38;5;241m224\u001b[39m, \u001b[38;5;241m224\u001b[39m, \u001b[38;5;241m3\u001b[39m),\n\u001b[1;32m      6\u001b[0m     include_top\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m)\n\u001b[1;32m     10\u001b[0m \u001b[38;5;66;03m# Freeze base model\u001b[39;00m\n",
      "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'tensorflow'"
     ]
    }
   ],
   "source": [
    "\n",
    "\n",
    "from tensorflow import keras\n",
    "\n",
    "base_model = keras.applications.VGG16(\n",
    "    weights=\"imagenet\",\n",
    "    input_shape=(224, 224, 3),\n",
    "    include_top=False)\n",
    "\n",
    "\n",
    "\n",
    "# Freeze base model\n",
    "base_model.trainable = False\n",
    "\n",
    "\n",
    "\n",
    "# Create inputs with correct shape\n",
    "inputs = keras.Input(shape=(224, 224, 3))\n",
    "\n",
    "x = base_model(inputs, training=False)\n",
    "\n",
    "# Add pooling layer or flatten layer\n",
    "x = keras.layers.GlobalAveragePooling2D()(x)\n",
    "\n",
    "# Add final dense layer\n",
    "outputs = keras.layers.Dense(6, activation = 'softmax')(x)\n",
    "\n",
    "# Combine inputs and outputs to create model\n",
    "model = keras.Model(inputs, outputs)\n",
    "\n",
    "\n",
    "\n",
    "model.summary()\n",
    "\n",
    "\n",
    "\n",
    "model.compile(loss='categorical_crossentropy', optimizer='adam',  metrics=['accuracy'])\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
    "# create a data generator\n",
    "datagen = ImageDataGenerator(\n",
    "        featurewise_center=True,  # set input mean to 0 over the dataset\n",
    "        samplewise_center=True,  # set each sample mean to 0\n",
    "        rotation_range=10,  # randomly rotate images in the range (degrees, 0 to 180)\n",
    "        zoom_range = 0.1, # Randomly zoom image \n",
    "        width_shift_range=0.1,  # randomly shift images horizontally (fraction of total width)\n",
    "        height_shift_range=0.1,  # randomly shift images vertically (fraction of total height)\n",
    "        horizontal_flip=True,  # randomly flip images\n",
    "        vertical_flip=False) # we don't expect Bo to be upside-down so we will not flip vertically\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "# load and iterate training dataset\n",
    "train_it = datagen.flow_from_directory(\"data/fruits/train\", \n",
    "                                       target_size=(224, 224), \n",
    "                                       color_mode='rgb', \n",
    "                                       class_mode=\"categorical\")\n",
    "# load and iterate validation dataset\n",
    "valid_it = datagen.flow_from_directory(\"data/fruits/valid\", \n",
    "                                      target_size=(224, 224), \n",
    "                                      color_mode='rgb', \n",
    "                                      class_mode=\"categorical\")\n",
    "\n",
    "\n",
    "\n",
    "model.fit(train_it,\n",
    "          validation_data=valid_it,\n",
    "          steps_per_epoch=train_it.samples/train_it.batch_size,\n",
    "          validation_steps=valid_it.samples/valid_it.batch_size,\n",
    "          epochs=10)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "38d72aad-e125-4e07-9b21-946321400f2a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "scpufreq(current=2650.12625, min=0.0, max=0.0)\n",
      "svmem(total=16468426752, available=14645440512, percent=11.1, used=1448964096, free=2567462912, active=977932288, inactive=11291680768, buffers=340774912, cached=12111224832, shared=15974400, slab=1280794624)\n",
      "[sdiskpart(device='/dev/root', mountpoint='/dli/assessment_results', fstype='ext4', opts='rw,relatime,discard', maxfile=255, maxpath=4096), sdiskpart(device='/dev/root', mountpoint='/dli/task', fstype='ext4', opts='rw,relatime,discard', maxfile=255, maxpath=4096), sdiskpart(device='/dev/root', mountpoint='/etc/resolv.conf', fstype='ext4', opts='rw,relatime,discard', maxfile=255, maxpath=4096), sdiskpart(device='/dev/root', mountpoint='/etc/hostname', fstype='ext4', opts='rw,relatime,discard', maxfile=255, maxpath=4096), sdiskpart(device='/dev/root', mountpoint='/etc/hosts', fstype='ext4', opts='rw,relatime,discard', maxfile=255, maxpath=4096), sdiskpart(device='/dev/root', mountpoint='/dli/task/data', fstype='ext4', opts='rw,relatime,discard', maxfile=255, maxpath=4096), sdiskpart(device='/dev/root', mountpoint='/usr/bin/nvidia-smi', fstype='ext4', opts='ro,nosuid,nodev,relatime,discard', maxfile=255, maxpath=4096), sdiskpart(device='/dev/root', mountpoint='/usr/bin/nvidia-debugdump', fstype='ext4', opts='ro,nosuid,nodev,relatime,discard', maxfile=255, maxpath=4096), sdiskpart(device='/dev/root', mountpoint='/usr/bin/nvidia-persistenced', fstype='ext4', opts='ro,nosuid,nodev,relatime,discard', maxfile=255, maxpath=4096), sdiskpart(device='/dev/root', mountpoint='/usr/bin/nvidia-cuda-mps-control', fstype='ext4', opts='ro,nosuid,nodev,relatime,discard', maxfile=255, maxpath=4096), sdiskpart(device='/dev/root', mountpoint='/usr/bin/nvidia-cuda-mps-server', fstype='ext4', opts='ro,nosuid,nodev,relatime,discard', maxfile=255, maxpath=4096), sdiskpart(device='/dev/root', mountpoint='/usr/lib/x86_64-linux-gnu/libnvidia-ml.so.525.85.12', fstype='ext4', opts='ro,nosuid,nodev,relatime,discard', maxfile=255, maxpath=4096), sdiskpart(device='/dev/root', mountpoint='/usr/lib/x86_64-linux-gnu/libnvidia-cfg.so.525.85.12', fstype='ext4', opts='ro,nosuid,nodev,relatime,discard', maxfile=255, maxpath=4096), sdiskpart(device='/dev/root', mountpoint='/usr/lib/x86_64-linux-gnu/libcuda.so.525.85.12', fstype='ext4', opts='ro,nosuid,nodev,relatime,discard', maxfile=255, maxpath=4096), sdiskpart(device='/dev/root', mountpoint='/usr/lib/x86_64-linux-gnu/libcudadebugger.so.525.85.12', fstype='ext4', opts='ro,nosuid,nodev,relatime,discard', maxfile=255, maxpath=4096), sdiskpart(device='/dev/root', mountpoint='/usr/lib/x86_64-linux-gnu/libnvidia-opencl.so.525.85.12', fstype='ext4', opts='ro,nosuid,nodev,relatime,discard', maxfile=255, maxpath=4096), sdiskpart(device='/dev/root', mountpoint='/usr/lib/x86_64-linux-gnu/libnvidia-ptxjitcompiler.so.525.85.12', fstype='ext4', opts='ro,nosuid,nodev,relatime,discard', maxfile=255, maxpath=4096), sdiskpart(device='/dev/root', mountpoint='/usr/lib/x86_64-linux-gnu/libnvidia-allocator.so.525.85.12', fstype='ext4', opts='ro,nosuid,nodev,relatime,discard', maxfile=255, maxpath=4096), sdiskpart(device='/dev/root', mountpoint='/usr/lib/x86_64-linux-gnu/libnvidia-compiler.so.525.85.12', fstype='ext4', opts='ro,nosuid,nodev,relatime,discard', maxfile=255, maxpath=4096), sdiskpart(device='/dev/root', mountpoint='/usr/lib/x86_64-linux-gnu/libnvidia-nvvm.so.525.85.12', fstype='ext4', opts='ro,nosuid,nodev,relatime,discard', maxfile=255, maxpath=4096), sdiskpart(device='/dev/root', mountpoint='/usr/lib/x86_64-linux-gnu/libnvidia-encode.so.525.85.12', fstype='ext4', opts='ro,nosuid,nodev,relatime,discard', maxfile=255, maxpath=4096), sdiskpart(device='/dev/root', mountpoint='/usr/lib/x86_64-linux-gnu/libnvidia-opticalflow.so.525.85.12', fstype='ext4', opts='ro,nosuid,nodev,relatime,discard', maxfile=255, maxpath=4096), sdiskpart(device='/dev/root', mountpoint='/usr/lib/x86_64-linux-gnu/libnvcuvid.so.525.85.12', fstype='ext4', opts='ro,nosuid,nodev,relatime,discard', maxfile=255, maxpath=4096)]\n"
     ]
    }
   ],
   "source": [
    "import psutil\n",
    "\n",
    "# CPU信息\n",
    "print(psutil.cpu_count())\n",
    "print(psutil.cpu_freq())\n",
    "\n",
    "# 内存信息\n",
    "print(psutil.virtual_memory())\n",
    "\n",
    "# 磁盘信息\n",
    "print(psutil.disk_partitions())\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "be364aad-edbb-4c99-8a01-33b11891d5ca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wed Nov 20 12:39:33 2024       \n",
      "+-----------------------------------------------------------------------------+\n",
      "| NVIDIA-SMI 525.85.12    Driver Version: 525.85.12    CUDA Version: 12.4     |\n",
      "|-------------------------------+----------------------+----------------------+\n",
      "| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |\n",
      "| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |\n",
      "|                               |                      |               MIG M. |\n",
      "|===============================+======================+======================|\n",
      "|   0  Tesla T4            On   | 00000000:00:1E.0 Off |                    0 |\n",
      "| N/A   30C    P0    26W /  70W |   1093MiB / 15360MiB |      0%      Default |\n",
      "|                               |                      |                  N/A |\n",
      "+-------------------------------+----------------------+----------------------+\n",
      "                                                                               \n",
      "+-----------------------------------------------------------------------------+\n",
      "| Processes:                                                                  |\n",
      "|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |\n",
      "|        ID   ID                                                   Usage      |\n",
      "|=============================================================================|\n",
      "+-----------------------------------------------------------------------------+\n"
     ]
    }
   ],
   "source": [
    "!nvidia-smi\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f8b5d932-6092-4cbd-89c2-4721e013988d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
